<?php
	$host="www.bzxz.net";
	$url_kind=array("bzxz");
	$url_num=0;

	$url="http://".$host."/".$url_kind[0]."/".$url_num.".html";
	$cookie="bzxznetecookieinforecord=%2C11-154498%2C12-110111%2C; UM_distinctid=15fb2ec20d6217-04fca3890d1506-173b7740-100200-15fb2ec20d71e2; CNZZDATA1726506=cnzz_eid%3D911064212-1510533226-%26ntime%3D1510544028; Hm_lvt_c9814f056e558d72a9729ea56950fc9a=1510535341; Hm_lpvt_c9814f056e558d72a9729ea56950fc9a=1510549426";
	function getheader(){
		$ip1=floor(rand(100,200));
		$ip2=floor(rand(100,200));
		$ip3=floor(rand(10,200));
		$ip4=floor(rand(10,200));
		$ip=$ip1.".".$ip2.".".$ip3.".".$ip4;
		$header=array(
		'Host: www.bzxz.net',
		'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0',
		'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
		'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
		'Cookie: bzxznetecookieinforecord=%2C11-154498%2C12-110111%2C; UM_distinctid=15fb2ec20d6217-04fca3890d1506-173b7740-100200-15fb2ec20d71e2; CNZZDATA1726506=cnzz_eid%3D911064212-1510533226-%26ntime%3D1510544028; Hm_lvt_c9814f056e558d72a9729ea56950fc9a=1510535341; Hm_lpvt_c9814f056e558d72a9729ea56950fc9a=1510549426',
		'Connection: keep-alive',
		'Upgrade-Insecure-Requests: 1',
		'X-FORWARDED-FOR:'.$ip,
		'CLIENT-IP:'.$ip
		);
		return $header;
	}



	function getHtml($url,$header,$cookie){
		$i=0;
		while($i<3){
		$ch=curl_init();
		//需要获取的 URL 地址，也可以在curl_init() 初始化会话的时候。
		curl_setopt($ch,CURLOPT_URL,$url);
		//TRUE 将curl_exec()获取的信息以字符串返回，而不是直接输出。
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		//设置 HTTP 头字段的数组。格式： array('Content-type: text/plain', 'Content-length: 100')
		curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
		//设定 HTTP 请求中"Cookie: "部分的内容。
		curl_setopt($ch,CURLOPT_COOKIE,$cookie);
		curl_setopt($ch,CURLOPT_TIMEOUT,15);
		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);
		$output=curl_exec($ch);
		if($output==false){
			$i++;
			echo $url."链接超时<br>";
			curl_close($ch);
		}else{
			curl_close($ch);
			$output=iconv("gbk","UTF-8//IGNORE",$output);
			return $output;
		}
		}
	}

	function write($str,$filename){
		$handle=fopen($filename,"a");
		fwrite($handle,$str);
		fwrite($handle,"\n");
		fclose($handle);
	}

	function zhengze($str){
		$patt='/<\/h3>.*?<script>.*?<script>/s';
		preg_match_all($patt,$str,$res);
		$str=$res[0][0];
		$patt='/<TD.*?>(<strong>|<b>|<p>|<SPAN.*?>|)(.*?)(<\/strong>|<\/b>|<\/p>|<\/SPAN>|)<\/TD>/';
		preg_match_all($patt,$str,$res);

		if (count($res[2])%2!=0){
			array_push($res[2],"");
		}
		return $res[2];

	}


	function read(){
		$filename="./main-1.txt";
		$handle=fopen($filename,"a");
		$content=fread($handle,filesize($filename));
		fclose($handle);
		return $content;
	}

	function save($arr){
		$res_k=array();
		$res_v=array();
		foreach ($arr as $k=>$v){
			if($k%2==0){
				$res_k[]=$v;
			}else{
				$res_v[]=$v;
			}
		}
		return array_combine($res_k,$res_v);
	}
	function test(){
		//$html=getHtml($url,$header,$cookie);
		$textname="./main-1.txt";
		//write($html,$textname);
		//echo "$url"."已完成"."";
		$content=read();
		//print_r($content);
		$res=zhengze($content);
		//print_r($res);
		$res_arr=save($res);
		$res_json=json_encode($res_arr);
		$savename="./save.txt";
		write($res_json,$savename);
		$conf=$url."写入完成";
		write($conf,"./conf.txt");
		echo $url."&nbsp;&nbsp;&nbsp;&nbsp;写入完成<br>";
	}

	function main($cookie){
			$host="www.bzxz.net";
			$url_kind=array("bzxz");
			$sleep_time=0;

		for($url_num=1;$url_num<=162010;$url_num++){
			$header=getheader();
			$url="http://".$host."/".$url_kind[0]."/".$url_num.".html";
			$html=getHtml($url,$header,$cookie);
			//print_r($header);
			$res=zhengze($html);
			$res_arr=save($res);
			$res_json=json_encode($res_arr);
			$savename="./save.txt";
			$confname="./conf.txt";
			//print_r(count($res_arr));
			if(count($res_arr)>10){
				write($res_json,$savename);
				$conf=$url."写入完成,请注意：".$url_num;
				write($conf,$confname);
				echo $url."&nbsp;&nbsp;&nbsp;&nbsp;写入完成".round(($url_num-160000)/(100),2)."%<br>";
			}else{
				$conf=$url."写入失败";
				write($conf,$confname);
				echo "<h3>".$url."&nbsp;&nbsp;&nbsp;&nbsp;写入失败</h3>";
			}

			$time=round(rand(0,1),2);
			$sleep_time+=$time;
			sleep($time);
		}
		return $sleep_time;
	}
	set_time_limit(0);
	$start_time=time();
	echo "开始时间：".date("H:i:s")."<br>";

	$sleep_time=main($cookie);

	$end_time=time();
	echo "开始时间：".date("H:i:s")."<br>";
	echo "结束时间：".date("H:i:s")."<br>";
	$d_time=$end_time-$start_time;
	echo "程序休眠时长:".$sleep_time."秒,   相当于：".floor($sleep_time/60)."分；<br>";
	echo "总计用时：".$d_time."秒"."<br>";
	echo "相当于:".floor($d_time/60)."分钟"."<br>";
	echo "相当于:".round($d_time/3600,2)."小时"."<br>";
	echo "实际用时:".(floor($d_time/60)-floor($sleep_time/60))."分钟"."<br>";
	echo "实际用时占比:".round(((floor($d_time/60)-floor($sleep_time/60))/floor($d_time/60)),2)*100 ."%";
	//print_r($ip);
	//110,82
?>
